package com.yangguang.huahui.ui.fragments;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.netease.scan.IScanModuleCallBack;
import com.netease.scan.QrScan;
import com.netease.scan.ui.CaptureActivity;
import com.yangguang.huahui.Config;
import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseQuickAdapter;
import com.yangguang.huahui.entity.Attention;
import com.yangguang.huahui.entity.AttentionList;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.mvp.model.CommonModel;
import com.yangguang.huahui.mvp.presenter.AttentionPresenter;
import com.yangguang.huahui.mvp.view.AttentionView;
import com.yangguang.huahui.rx.RxManager;
import com.yangguang.huahui.rx.RxSubscriber;
import com.yangguang.huahui.ui.activitys.ChangePinActivity;
import com.yangguang.huahui.ui.activitys.ClientNeedsActivity;
import com.yangguang.huahui.ui.activitys.DetailActivity;
import com.yangguang.huahui.ui.activitys.FindFlowersActivity;
import com.yangguang.huahui.ui.activitys.FindMiaomuActivity;
import com.yangguang.huahui.ui.activitys.LoginActivity;
import com.yangguang.huahui.ui.activitys.MainActivity;
import com.yangguang.huahui.ui.activitys.NongjiActivity;
import com.yangguang.huahui.ui.activitys.NongziActivity;
import com.yangguang.huahui.ui.activitys.PublishActivity;
import com.yangguang.huahui.ui.activitys.QuotesActivity;
import com.yangguang.huahui.ui.activitys.ReplyActivity;
import com.yangguang.huahui.ui.activitys.WelcomeActivity;
import com.yangguang.huahui.ui.adapters.BGABannerAdapter;
import com.yangguang.huahui.ui.adapters.MiaomuAdapter;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.LogUtil;
import com.yangguang.huahui.widget.FullyLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by Administrator on 2017/3/27.
 */

public class FirstFragment extends BaseMvpFragment<AttentionView, AttentionPresenter> implements AttentionView, View.OnClickListener {
    @BindView(R.id.bsv_home)
    NestedScrollView bsv_home;
    //    @BindView(R.id.mBgaBanner)
    BGABanner mBgaBanner;
    //    @BindView(R.id.mTabLayout)
    TabLayout mTabLayout;
    //    @BindView(R.id.mLL_attention)
    LinearLayout mLL_attention;
    //    @BindView(R.id.tv_pinlei)
    TextView tv_pinlei;
    private int layoutTop;
    @BindView(R.id.mLL_top)
    LinearLayout mLL_top;
    private Attention attention;


    public static FirstFragment newInstance(String type) {
        FirstFragment fragment = new FirstFragment();
        Bundle arguments = new Bundle();
        arguments.putString("type", type);
        fragment.setArguments(arguments);
        return fragment;
    }

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private LinearLayoutManager layoutManager;
    private MiaomuAdapter mAdapter;
    private List<AttentionList.ListBean> datas = new ArrayList<>();

    @Override
    public void onSuccess(AttentionList data) {
        if (data.getList() != null) {
            datas = data.getList();
        }
        mAdapter.setNewData(datas);
    }

    @Override
    public void onError() {

    }

    @Override
    protected AttentionPresenter initPresenter() {
        return new AttentionPresenter();
    }

    @Override
    protected void fetchData() {
    }

    @Override
    protected int initLayoutId() {
        return R.layout.frag_first;
    }


//    @OnClick({R.id.mLL_miaomu, R.id.tv_miaomu, R.id.mLL_flowers, R.id.tv_flowers, R.id.mLL_nongji, R.id.tv_nongji,
//            R.id.mLL_nongzi, R.id.tv_nongzi, R.id.mLL_client_needs, R.id.tv_client_needs, R.id.mLL_publish,
//            R.id.tv_publish, R.id.tv_pinlei, R.id.mLL_market, R.id.tv_market, R.id.mLL_reply, R.id.tv_reply})

    private void initEvent(View view) {
        view.findViewById(R.id.mLL_miaomu).setOnClickListener(this);
        view.findViewById(R.id.tv_miaomu).setOnClickListener(this);
        view.findViewById(R.id.mLL_flowers).setOnClickListener(this);
        view.findViewById(R.id.tv_flowers).setOnClickListener(this);
        view.findViewById(R.id.mLL_nongji).setOnClickListener(this);
        view.findViewById(R.id.tv_nongji).setOnClickListener(this);
        view.findViewById(R.id.mLL_nongzi).setOnClickListener(this);
        view.findViewById(R.id.tv_nongzi).setOnClickListener(this);
        view.findViewById(R.id.tv_nongzi).setOnClickListener(this);
        view.findViewById(R.id.mLL_client_needs).setOnClickListener(this);
        view.findViewById(R.id.tv_client_needs).setOnClickListener(this);
        view.findViewById(R.id.mLL_publish).setOnClickListener(this);
        view.findViewById(R.id.tv_publish).setOnClickListener(this);
        view.findViewById(R.id.tv_pinlei).setOnClickListener(this);
        view.findViewById(R.id.mLL_market).setOnClickListener(this);
        view.findViewById(R.id.tv_market).setOnClickListener(this);
        view.findViewById(R.id.mLL_reply).setOnClickListener(this);
        view.findViewById(R.id.tv_reply).setOnClickListener(this);

    }

    @OnClick({R.id.mLL_sao})
    void click(View v){
        switch (v.getId()){
            case R.id.mLL_sao:
                mActivity.requestPermission(new String[]{Manifest.permission.CAMERA}, 0x0001);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mLL_miaomu:
                startActivity(new Intent(mActivity, FindMiaomuActivity.class));
                break;
            case R.id.tv_miaomu://找苗木
                startActivity(new Intent(mActivity, FindMiaomuActivity.class));
                break;
            case R.id.mLL_flowers:
                startActivity(new Intent(mActivity, FindFlowersActivity.class));
                break;
            case R.id.tv_flowers://找花卉
                startActivity(new Intent(mActivity, FindFlowersActivity.class));
                break;
            case R.id.mLL_nongji:
                startActivity(new Intent(mActivity, NongjiActivity.class));
                break;
            case R.id.tv_nongji://找农机
                startActivity(new Intent(mActivity, NongjiActivity.class));
                break;
            case R.id.mLL_nongzi:
                startActivity(new Intent(mActivity, NongziActivity.class));
                break;
            case R.id.tv_nongzi://找农资
                startActivity(new Intent(mActivity, NongziActivity.class));
                break;
            case R.id.mLL_client_needs:
                startActivity(new Intent(mActivity, ClientNeedsActivity.class));
                break;
            case R.id.tv_client_needs://客户需求
                startActivity(new Intent(mActivity, ClientNeedsActivity.class));
                break;
            case R.id.mLL_publish:
                if (CommonUtil.isNull(DataInfo.TOKEN))
                    startActivity(new Intent(mActivity, LoginActivity.class));
                else startActivity(new Intent(mActivity, PublishActivity.class));
                break;
            case R.id.tv_publish://发布产品
                if (CommonUtil.isNull(DataInfo.TOKEN))
                    startActivity(new Intent(mActivity, LoginActivity.class));
                else startActivity(new Intent(mActivity, PublishActivity.class));
                break;
            case R.id.tv_pinlei:
                if (attention == null) return;
                startActivity(new Intent(mActivity, ChangePinActivity.class).putExtra("attention", attention));
                break;
            case R.id.mLL_market:
                startActivity(new Intent(mActivity, QuotesActivity.class));
                break;
            case R.id.tv_market://行情
                startActivity(new Intent(mActivity, QuotesActivity.class));
                break;
            case R.id.mLL_reply:
                startActivity(new Intent(mActivity, ReplyActivity.class)
                        .putExtra("type", 1).putExtra("title", getString(R.string.question_reply)));
                break;
            case R.id.tv_reply://问答
                startActivity(new Intent(mActivity, ReplyActivity.class)
                        .putExtra("type", 1).putExtra("title", getString(R.string.question_reply)));
                break;
        }
    }

    @Override
    protected void initView() {
        getTab();
        layoutManager = new FullyLinearLayoutManager(mActivity);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setNestedScrollingEnabled(false);
        mAdapter = new MiaomuAdapter(R.layout.find_miaomu_list_item, datas);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRecyclerView.setAdapter(mAdapter);
        View headView = LayoutInflater.from(mActivity).inflate(R.layout.home_head_layout, null);
        mTabLayout = (TabLayout) headView.findViewById(R.id.mTabLayout);
        mBgaBanner = (BGABanner) headView.findViewById(R.id.mBgaBanner);
        mLL_attention = (LinearLayout) headView.findViewById(R.id.mLL_attention);
        tv_pinlei = (TextView) headView.findViewById(R.id.tv_pinlei);

        initBgaBanner();
        mAdapter.addHeaderView(headView);
        initEvent(headView);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(new Intent(mActivity, DetailActivity.class)
                        .putExtra("webUrl", Config.SUPPLY_DETAIL)
                        .putExtra("id", mAdapter.getItem(position).getId())
                        .putExtra("detail_type", "2"));
            }
        });
        bsv_home.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (mLL_attention != null) {
                    layoutTop = mLL_attention.getTop();
                }
                setTop(scrollY);
            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        bsv_home.scrollTo(0, 0);
    }

    /**
     * 已滑动的距离
     */
    private int getDistance() {
        LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
        View firstVisibItem = mRecyclerView.getChildAt(0);
        int firstItemPosition = layoutManager.findFirstVisibleItemPosition();
        int itemHeight = firstVisibItem.getHeight();
        int firstItemBottom = layoutManager.getDecoratedBottom(firstVisibItem);
        return (firstItemPosition + 1) * itemHeight - firstItemBottom;
    }

    private void setTop(int scrollY) {
        if (scrollY >= layoutTop) {
            if (mTabLayout.getParent() != mLL_top) {
                mLL_attention.removeView(mTabLayout);
                mLL_attention.removeView(tv_pinlei);
                mLL_top.addView(mTabLayout);
                mLL_top.addView(tv_pinlei);
            }
        } else {
            if (mTabLayout.getParent() != mLL_attention) {
                mLL_top.removeView(mTabLayout);
                mLL_top.removeView(tv_pinlei);
                mLL_attention.addView(mTabLayout);
                mLL_attention.addView(tv_pinlei);
            }
        }
    }

    @Override
    protected void initData() {

    }

    List<Attention.ListBean> tabList = new ArrayList<>();

    private void getTab() {
        RxManager.getInstance().doSubscribe(new CommonModel().getTabs(), new RxSubscriber<Attention>(false) {
            @Override
            protected void _onNext(Attention result) {
                attention = result;
                tabList = attention.getList();
                mPresenter.getListData(tabList.get(0).getId());
                mTabLayout.removeAllTabs();
                mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                for (int i = 0; i < attention.getList().size(); i++) {
                    mTabLayout.addTab(mTabLayout.newTab().setText(attention.getList().get(i).getClassname()));
                }
                mTabLayout.addOnTabSelectedListener(selectedListener);
            }

            @Override
            protected void _onError() {
            }
        });
    }

    TabLayout.OnTabSelectedListener selectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mPresenter.getListData(tabList.get(tab.getPosition()).getId());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    };

    private void initBgaBanner() {
        List<String> banners = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        titles.add("");
        titles.add("");
        titles.add("");
        titles.add("");
        banners.add("http://www.cnitlabor.com/Public/Uploads/5875a9ff15fd2.png");
        banners.add("http://www.cnitlabor.com/Public/Uploads/58a43d95074b5.png");
        banners.add("http://www.cnitlabor.com/Public/Uploads/58a65ba792419.png");
        banners.add("http://www.cnitlabor.com/Public/Uploads/586b8ca4ceae1.png");
        mBgaBanner.setAdapter(new BGABannerAdapter(mActivity));
        mBgaBanner.setData(banners, titles);
    }
}
